package io.renren.modules.simida.service.impl;

import io.renren.common.utils.Constant;
import io.renren.common.utils.StringUtil;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.simida.entity.UserInfoEntity;
import io.renren.modules.simida.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.simida.dao.UserMakeCommentDao;
import io.renren.modules.simida.entity.UserMakeCommentEntity;
import io.renren.modules.simida.service.UserMakeCommentService;


@Service("userMakeCommentService")
public class UserMakeCommentServiceImpl extends ServiceImpl<UserMakeCommentDao, UserMakeCommentEntity> implements UserMakeCommentService {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserMakeCommentDao userMakeCommentDao;
    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<UserMakeCommentEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("state",1);
        queryWrapper.eq(StringUtil.isNotEmpty(params.get("targetUserId")),"target_user_id",params.get("targetUserId"));
        queryWrapper.orderByDesc("create_time");
        IPage<UserMakeCommentEntity> page = this.page(
                new Query<UserMakeCommentEntity>().getPage(params),
                queryWrapper
        );
        List<UserMakeCommentEntity> records = page.getRecords();
        for (UserMakeCommentEntity record : records) {
            UserInfoEntity userInfoServiceById = userInfoService.getById(record.getUserId());
            record.setNickname(userInfoServiceById.getNickname());
            record.setAvatar(userInfoServiceById.getAvatar());
        }
        return new PageUtils(page);
    }

    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public void deleteById(Long id) {
        userMakeCommentDao.updateStateById(id);
    }

}